!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine weno_ly (fyro, fymx, fymy, fymz, fybx, fyby, fybz, fyen, fypo,                                                          &
   ro, mx, my, mz, bx, by, bz, en, po, nx, ny, gm, ch2, riemann_solver_flag)
!==================================================================================================================================|

   implicit none

   integer(4), intent(in) :: nx, ny, riemann_solver_flag

   real(8), intent(in) :: gm, ch2
   real(8), dimension(nx, ny), intent(in) :: ro, mx, my, mz, bx, by, bz, en, po
   real(8), dimension(nx, ny), intent(inout) :: fyro, fymx, fymy, fymz, fybx, fyby, fybz, fyen, fypo

   integer(4) :: i, j, jp1, jm1

   real(8), dimension(nx, ny) :: ro_l, mx_l, my_l, mz_l, bx_l, by_l, bz_l, en_l, po_l
   real(8), dimension(nx, ny) :: ro_r, mx_r, my_r, mz_r, bx_r, by_r, bz_r, en_r, po_r
!   real(8), dimension(nx, ny) :: s

   real(8) :: fyro_l, fymx_l, fymy_l, fymz_l, fybx_l, fyby_l, fybz_l, fyen_l, fypo_l
   real(8) :: fyro_r, fymx_r, fymy_r, fymz_r, fybx_r, fyby_r, fybz_r, fyen_r, fypo_r
   real(8) :: alpha_0, alpha_1, d_0, d_1, var_0, var_1
   real(8) :: eps, gmm1, vx, vy, vz, b2, v2, pr, max_speed, c2, s2, ca2, cfy

!----------------------------------------------------------------------------------------------------------------------------------|
   eps = 1.0d-12
   gmm1 = gm - 1.0d0

   d_0 = 2.0d0 / 3.0d0
   d_1 = 1.0d0 / 3.0d0
   do j = 2, ny - 1
      jp1 = j + 1
      jm1 = j - 1
      do i = 1, nx
         var_0 = 0.5d0 * (ro(i, j) + ro(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * ro(i, j) - ro(i, jm1))
         alpha_0 = d_0 / ((eps + (ro(i, jp1) - ro(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (ro(i, j) - ro(i, jm1)) ** 2) ** 2)
         ro_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (mx(i, j) + mx(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * mx(i, j) - mx(i, jm1))
         alpha_0 = d_0 / ((eps + (mx(i, jp1) - mx(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (mx(i, j) - mx(i, jm1)) ** 2) ** 2)
         mx_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (my(i, j) + my(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * my(i, j) - my(i, jm1))
         alpha_0 = d_0 / ((eps + (my(i, jp1) - my(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (my(i, j) - my(i, jm1)) ** 2) ** 2)
         my_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (mz(i, j) + mz(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * mz(i, j) - mz(i, jm1))
         alpha_0 = d_0 / ((eps + (mz(i, jp1) - mz(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (mz(i, j) - mz(i, jm1)) ** 2) ** 2)
         mz_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (bx(i, j) + bx(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * bx(i, j) - bx(i, jm1))
         alpha_0 = d_0 / ((eps + (bx(i, jp1) - bx(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (bx(i, j) - bx(i, jm1)) ** 2) ** 2)
         bx_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (by(i, j) + by(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * by(i, j) - by(i, jm1))
         alpha_0 = d_0 / ((eps + (by(i, jp1) - by(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (by(i, j) - by(i, jm1)) ** 2) ** 2)
         by_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (bz(i, j) + bz(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * bz(i, j) - bz(i, jm1))
         alpha_0 = d_0 / ((eps + (bz(i, jp1) - bz(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (bz(i, j) - bz(i, jm1)) ** 2) ** 2)
         bz_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (en(i, j) + en(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * en(i, j) - en(i, jm1))
         alpha_0 = d_0 / ((eps + (en(i, jp1) - en(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (en(i, j) - en(i, jm1)) ** 2) ** 2)
         en_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (po(i, j) + po(i, jp1))
         var_1 = 0.5d0 * (3.0d0 * po(i, j) - po(i, jm1))
         alpha_0 = d_0 / ((eps + (po(i, jp1) - po(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (po(i, j) - po(i, jm1)) ** 2) ** 2)
         po_l(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)
      enddo
   enddo

   d_0 = 1.0d0 / 3.0d0
   d_1 = 2.0d0 / 3.0d0
   do j = 2, ny - 1
      jp1 = j + 1
      jm1 = j - 1
      do i = 1, nx
         var_0 = 0.5d0 * (3.0d0 * ro(i, j) - ro(i, jp1))
         var_1 = 0.5d0 * (ro(i, j) + ro(i, jm1))
         alpha_0 = d_0 / ((eps + (ro(i, jp1) - ro(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (ro(i, j) - ro(i, jm1)) ** 2) ** 2)
         ro_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * mx(i, j) - mx(i, jp1))
         var_1 = 0.5d0 * (mx(i, j) + mx(i, jm1))
         alpha_0 = d_0 / ((eps + (mx(i, jp1) - mx(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (mx(i, j) - mx(i, jm1)) ** 2) ** 2)
         mx_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * my(i, j) - my(i, jp1))
         var_1 = 0.5d0 * (my(i, j) + my(i, jm1))
         alpha_0 = d_0 / ((eps + (my(i, jp1) - my(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (my(i, j) - my(i, jm1)) ** 2) ** 2)
         my_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * mz(i, j) - mz(i, jp1))
         var_1 = 0.5d0 * (mz(i, j) + mz(i, jm1))
         alpha_0 = d_0 / ((eps + (mz(i, jp1) - mz(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (mz(i, j) - mz(i, jm1)) ** 2) ** 2)
         mz_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * bx(i, j) - bx(i, jp1))
         var_1 = 0.5d0 * (bx(i, j) + bx(i, jm1))
         alpha_0 = d_0 / ((eps + (bx(i, jp1) - bx(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (bx(i, j) - bx(i, jm1)) ** 2) ** 2)
         bx_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * by(i, j) - by(i, jp1))
         var_1 = 0.5d0 * (by(i, j) + by(i, jm1))
         alpha_0 = d_0 / ((eps + (by(i, jp1) - by(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (by(i, j) - by(i, jm1)) ** 2) ** 2)
         by_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * bz(i, j) - bz(i, jp1))
         var_1 = 0.5d0 * (bz(i, j) + bz(i, jm1))
         alpha_0 = d_0 / ((eps + (bz(i, jp1) - bz(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (bz(i, j) - bz(i, jm1)) ** 2) ** 2)
         bz_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * en(i, j) - en(i, jp1))
         var_1 = 0.5d0 * (en(i, j) + en(i, jm1))
         alpha_0 = d_0 / ((eps + (en(i, jp1) - en(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (en(i, j) - en(i, jm1)) ** 2) ** 2)
         en_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)

         var_0 = 0.5d0 * (3.0d0 * po(i, j) - po(i, jp1))
         var_1 = 0.5d0 * (po(i, j) + po(i, jm1))
         alpha_0 = d_0 / ((eps + (po(i, jp1) - po(i, j)) ** 2) ** 2)
         alpha_1 = d_1 / ((eps + (po(i, j) - po(i, jm1)) ** 2) ** 2)
         po_r(i, j) = (alpha_0 * var_0 + alpha_1 * var_1) / (alpha_0 + alpha_1)
      enddo
   enddo

   if (riemann_solver_flag .eq. 1) then
      call hlld_y (fyro, fymy, fymx, fymz, fyby, fybx, fybz, fyen,                                                                 &
      ro_l, my_l, mx_l, mz_l, by_l, bx_l, bz_l, en_l, ro_r, my_r, mx_r, mz_r, by_r, bx_r, bz_r, en_r, nx, ny, gm)
   elseif (riemann_solver_flag .eq. 2) then
      call hllc_y (fyro, fymy, fymx, fymz, fyby, fybx, fybz, fyen,                                                                 &
      ro_l, my_l, mx_l, mz_l, by_l, bx_l, bz_l, en_l, ro_r, my_r, mx_r, mz_r, by_r, bx_r, bz_r, en_r, nx, ny, gm)
   elseif (riemann_solver_flag .eq. 3) then
      call roe_y (fyro, fymy, fymx, fymz, fyby, fybx, fybz, fyen,                                                                 &
      ro_l, my_l, mx_l, mz_l, by_l, bx_l, bz_l, en_l, ro_r, my_r, mx_r, mz_r, by_r, bx_r, bz_r, en_r, nx, ny, gm)
   endif

   do j = 1, ny - 1
      jp1 = j + 1
!      jm1 = max (j - 1, 1)
      do i = 1, nx
!         max_speed = maxval (s(i, jm1 : jp1))

         vx = mx_l(i, j) / ro_l(i, j)
         vy = my_l(i, j) / ro_l(i, j)
         vz = mz_l(i, j) / ro_l(i, j)
         b2 = bx_l(i, j) * bx_l(i, j) + by_l(i, j) * by_l(i, j) + bz_l(i, j) * bz_l(i, j)
         v2 = vx * vx + vy * vy + vz * vz
         pr = (en_l(i, j) - v2 * ro_l(i, j) / 2.0d0 - b2 / 2.0d0) * gmm1
         c2 = gm * pr
         s2 = c2 + b2
         ca2 = by_l(i, j) * by_l(i, j)
         cfy = sqrt ((s2 + sqrt (s2 * s2 - 4.0d0 * c2 * ca2)) / ro_l(i, j) / 2.0d0)
         max_speed = cfy + abs (vy)

         if (riemann_solver_flag .eq. 0) then
            fyro_l = my_l(i, j)
            fymx_l = vy * mx_l(i, j) - by_l(i, j) * bx_l(i, j)
            fymy_l = vy * my_l(i, j) + pr + b2 / 2.0d0 - by_l(i, j) * by_l(i, j)
            fymz_l = vy * mz_l(i, j) - by_l(i, j) * bz_l(i, j)
            fybx_l = vy * bx_l(i, j) - by_l(i, j) * vx
            fyby_l = po_l(i, j)
            fybz_l = vy * bz_l(i, j) - by_l(i, j) * vz
            fyen_l = (en_l(i, j) + pr + b2 / 2.0d0) * vy - (bx_l(i, j) * vx + by_l(i, j) * vy + bz_l(i, j) * vz) * by_l(i, j)
         endif
         fypo_l = ch2 * by_l(i, j)

         vx = mx_r(i, jp1) / ro_r(i, jp1)
         vy = my_r(i, jp1) / ro_r(i, jp1)
         vz = mz_r(i, jp1) / ro_r(i, jp1)
         b2 = bx_r(i, jp1) * bx_r(i, jp1) + by_r(i, jp1) * by_r(i, jp1) + bz_r(i, jp1) * bz_r(i, jp1)
         v2 = vx * vx + vy * vy + vz * vz
         pr = (en_r(i, jp1) - v2 * ro_r(i, jp1) / 2.0d0 - b2 / 2.0d0) * gmm1
         c2 = gm * pr
         s2 = c2 + b2
         ca2 = by_r(i, jp1) * by_r(i, jp1)
         cfy = sqrt ((s2 + sqrt (s2 * s2 - 4.0d0 * c2 * ca2)) / ro_r(i, jp1) / 2.0d0)
         max_speed = max (cfy + abs (vy), max_speed)

         if (riemann_solver_flag .eq. 0) then
            fyro_r = my_r(i, jp1)
            fymx_r = vy * mx_r(i, jp1) - by_r(i, jp1) * bx_r(i, jp1)
            fymy_r = vy * my_r(i, jp1) + pr + b2 / 2.0d0 - by_r(i, jp1) * by_r(i, jp1)
            fymz_r = vy * mz_r(i, jp1) - by_r(i, jp1) * bz_r(i, jp1)
            fybx_r = vy * bx_r(i, jp1) - by_r(i, jp1) * vx
            fyby_r = po_r(i, jp1)
            fybz_r = vy * bz_r(i, jp1) - by_r(i, jp1) * vz
            fyen_r = (en_r(i, jp1) + pr + b2 / 2.0d0) * vy - (bx_r(i, jp1) * vx + by_r(i, jp1) * vy + bz_r(i, jp1) * vz) *         &
               by_r(i, jp1)
         endif
         fypo_r = ch2 * by_r(i, jp1)

         if (riemann_solver_flag .eq. 0) then
            fyro(i, j) = 0.5d0 * (fyro_l + fyro_r - max_speed * (ro_r(i, jp1) - ro_l(i, j)))
            fymx(i, j) = 0.5d0 * (fymx_l + fymx_r - max_speed * (mx_r(i, jp1) - mx_l(i, j)))
            fymy(i, j) = 0.5d0 * (fymy_l + fymy_r - max_speed * (my_r(i, jp1) - my_l(i, j)))
            fymz(i, j) = 0.5d0 * (fymz_l + fymz_r - max_speed * (mz_r(i, jp1) - mz_l(i, j)))
            fybx(i, j) = 0.5d0 * (fybx_l + fybx_r - max_speed * (bx_r(i, jp1) - bx_l(i, j)))
            fyby(i, j) = 0.5d0 * (fyby_l + fyby_r - max_speed * (by_r(i, jp1) - by_l(i, j)))
            fybz(i, j) = 0.5d0 * (fybz_l + fybz_r - max_speed * (bz_r(i, jp1) - bz_l(i, j)))
            fyen(i, j) = 0.5d0 * (fyen_l + fyen_r - max_speed * (en_r(i, jp1) - en_l(i, j)))
         endif
         fypo(i, j) = 0.5d0 * (fypo_l + fypo_r - max_speed * (po_r(i, jp1) - po_l(i, j)))
      enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine weno_ly
